//
//  LiveViewController.m
//  梁森直播
//
//  Created by 中创 on 2020/4/27.
//  Copyright © 2020 LS. All rights reserved.
//

#import "LiveViewController.h"

@import TXLiteAVSDK_Professional;

/// 直播页面
@interface LiveViewController ()

@property (nonatomic, strong) TXLivePush * pusher;

@end

@implementation LiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
        [self preparePush];
        [self setupUI];
}

- (void)setupUI{
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton * startBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [startBtn setTitle:@"开启直播" forState:UIControlStateNormal];
    startBtn.frame = CGRectMake(20, 100, 80, 30);
    [startBtn addTarget:self action:@selector(startPush) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:startBtn];
    UIButton * stopBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [stopBtn setTitle:@"结束直播" forState:UIControlStateNormal];
    stopBtn.frame = CGRectMake(200, 100, 80, 30);
    [stopBtn addTarget:self action:@selector(stopPush) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:stopBtn];
}
/// 准备直播
- (void)preparePush{
    //  初始化 TXLivePush 组件
    TXLivePushConfig *_config = [[TXLivePushConfig alloc] init];  // 一般情况下不需要修改默认 config
    TXLivePush *_pusher = [[TXLivePush alloc] initWithConfig: _config]; // config 参数不能为空
    self.pusher = _pusher;
}

/// 开始直播
- (void)startPush{
    // 开启摄像头预览
    //创建一个 view 对象，并将其嵌入到当前界面中
    UIView *_localView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view insertSubview:_localView atIndex:0];
    _localView.center = self.view.center;

    //启动本地摄像头预览
    [_pusher startPreview:_localView];
    // 开启推流
    NSString* rtmpUrl = @"rtmp://94642.livepush.myqcloud.com/kantianxia/kantianxiaStream?txSecret=b41aa6ecb1a4e4b4dfa5ee8bda3dd4b4&txTime=5EA7017F";    //此处填写您的 rtmp 推流地址
    [self.pusher startPush:rtmpUrl];
}
/// 结束直播
- (void)stopPush{
    [self.pusher stopPreview]; //如果已经启动了摄像头预览，请在结束推流时将其关闭。
    [self.pusher stopPush];
}


@end
